package es.mgil.ejercicios.clase;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class DibujarAvanzado2 extends Activity implements OnTouchListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		RenderView rv = new RenderView(this);
		setContentView(rv);
		rv.setOnTouchListener(this);
	}

	private class RenderView extends View {
		private Paint paint;
		private int line_end_x;
		private int line_end_y;
		private boolean up;
		private Random random;
		private int maxWidth;
		private int maxHeight;
		private Runnable runnable;
		private Thread thread;
		private Canvas canvas;

		public RenderView(Context context) {
			super(context);
			paint = new Paint();
			random = new Random();

			Point point = new Point();
			getWindowManager().getDefaultDisplay().getSize(point);
			maxWidth = point.x;
			maxHeight = point.y;
			line_end_x = maxWidth - 1;
			line_end_y = 0;
			up = false;

			runnable = new Runnable() {
				@Override
				public void run() {
					paint();

					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
					} finally {
						invalidate();
					}
				}
			};
			thread = new Thread(runnable);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			this.canvas = canvas;
			thread.run();
		}

		public void paint() {
			// draw the background color
			canvas.drawRGB(255, 255, 255);

			{
				// draw a movable line
				if (line_end_x == canvas.getWidth() - 1) {
					if (up) {
						line_end_y--;
					} else {
						line_end_y++;
					}
				}

				if (line_end_y == canvas.getHeight() - 1) {
					if (up) {
						line_end_x++;
					} else {
						line_end_x--;
					}
				}

				if (line_end_x == 0 && line_end_y == canvas.getHeight() - 1) {
					up = true;
				} else if (line_end_x == canvas.getWidth() - 1
						&& line_end_y == 0) {
					up = false;
				}

				paint.setColor(Color.BLUE);
				canvas.drawLine(0, 0, line_end_x, line_end_y, paint);
			}

			{
				// draw a circle
				int x = random.nextInt(maxWidth);
				int y = random.nextInt(maxHeight);
				int r = Math.max(
						1,
						Math.min(Math.min(maxWidth - x, maxHeight - y),
								Math.min(x, y)));
				int radius = random.nextInt(r);
				paint.setStyle(Style.FILL_AND_STROKE);
				paint.setColor(0xff00ff00);
				canvas.drawCircle(x, y, radius, paint);
			}

			{
				// draw a rectangle
				int startX = random.nextInt(maxWidth);
				int startY = random.nextInt(maxHeight);
				int endX = random.nextInt(Math.max(startX, maxWidth));
				int endY = random.nextInt(Math.max(startY, maxHeight));

				paint.setStyle(Style.FILL);
				paint.setColor(Color.RED);
				paint.setAlpha(0xcc);
				canvas.drawRect(startX, startY, endX, endY, paint);
			}
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			finish();
			break;
		}

		return true;
	}
}
